package com.vogue.supplymall.shop.domain.dao.impl;

import com.vogue.supplymall.common.domain.dto.SearchInfo;
import com.vogue.supplymall.shop.domain.dao.ShopDao;
import com.vogue.supplymall.shop.domain.mapper.ShopMapper;
import com.vogue.supplymall.shop.domain.model.Shop;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Shop dao.
 */
@Repository("shopDao")
public class ShopDaoImpl extends BaseDaoImpl<Shop, Long> implements ShopDao {

    @Resource
    private ShopMapper shopMapper;

    @Override
    public List<Shop> findShopForSql(Long sysCodeId, Long organId) {
        return shopMapper.findShop(sysCodeId, organId);
    }

    @Override
	public List<Shop> findShopByMS(Long syscodeId, String matchId,int tendId) {
		
		return shopMapper.findShopByMS(syscodeId, matchId,tendId);
	}

	@Override
    public List<Map<String, Object>> findShop(SearchInfo searchInfo) {
        return shopMapper.findShopBySql(searchInfo);
    }
    
	@Override
	public void changeHiddenFlagOfShop(Long shopId, Long sysCodeId, int hiddenFlag) {
		String sql="update shop set hiddenflag="+hiddenFlag 
				 +" where sysCodeId="+sysCodeId +" and id="+shopId;
		this.update(sql);
		
	}

	@Override
	public List<Map<String, Object>> findLicenceShopBySql(SearchInfo searchInfo) throws RuntimeException {
		StringBuilder sql=new StringBuilder();
    	sql.append("SELECT")
    		.append(" s.id,")
    		.append(" s.name,")
    		.append(" s.syscodeid,")
    		.append(" -1 as applystatue")
		.append(" FROM")
    		.append(" shop s")
		.append(" JOIN")
    		.append(" administrativearea asa")
		.append(" ON")
			.append(" s.area=asa.areacode and s.province=asa.id")
		.append(" where")
			.append(" s.syscodeid=#{syscodeid} ")
			.append(" and s.disabledflag=1 and s.stopflag = 0 ")
			.append(searchInfo.getParamCol()!=null && !"".equals(searchInfo.getParamCol()) ? " and s.matchid not in (#{paramCol}) ":"")
			.append(" and (s.name like CONCAT('%',#{content},'%')")
			.append(" or asa.name like CONCAT('%',#{content},'%')")
			.append(" or asa.areaname like CONCAT('%',#{content},'%')) ")
		.append(" ORDER BY")
			.append(" convert(asa.areaname using gbk),")
			.append(" convert(asa.name using gbk),")
			.append(" convert(s.name using gbk) asc")
    	.append(" LIMIT #{start},#{length}");
    	Map<String, Object> map = new HashMap<>();
    	map.put("syscodeid", searchInfo.getSyscodeid());
    	map.put("paramCol", searchInfo.getParamCol() != null ? Arrays.asList(searchInfo.getParamCol().split(",")) : "0");
    	map.put("content", searchInfo.getContent());
    	map.put("start", searchInfo.getStart());
    	map.put("length", searchInfo.getLength());
		return shopMapper.query0(sql.toString(), map);
	}

	@Override
	public Long countValidShop() {
		return shopMapper.countValidShop();
	}

	@Override
	public void updateWaitingOrderBySyscodeId(Long syscodeId, int waitingOrder) {
		String sql = "update shop set waitingorder=#{waitingorder} where syscodeid=#{syscodeid}";
		Map<String, Object> map = new HashMap<>();
		map.put("waitingorder", waitingOrder);
		map.put("syscodeid", syscodeId);
		this.update0(sql, map);
	}

	@Override
	public Long findShopCount(SearchInfo searchInfo) {
		// TODO Auto-generated method stub
		return shopMapper.findShopCountBySql(searchInfo);
	}
}